﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;
using GRWL.BZSC.UI.CustomCtrl;
using UserCtrl;

namespace GRWL.BZSC.UI
{
    //请购管理
    public partial class RequisitionFrm : BaseFrm
    {
        //public CodeRuleFrm.CodeRule codeRule { get; set; }

        //public GRWL.BZSC.UI.Common.BillOperateType EditStatus;

        public TB_Requisition Requisition;
        public TB_ReqDetail[] ReqDetail;

        public XRequisition XReqMain;
        public XReqDetial[] XDetials;


        private TB_Trader Trader;
        private TB_Department Department;
        private TB_Department[] Departments;

        private TB_User[] Users;
        private XUser[] XUsers;

        private DataTable tXUsers;

        bool bEdit, bConfirm, bPrint;

        /// <summary>
        /// 临时变量日期控件
        /// </summary>
        DateTimePicker _DTP = new DateTimePicker();
        UDataGridView theView;

        public override void EditStatusChanged()
        {
            RefreshStatus();
            //btnAppend.Enabled = (EditStatus != Common.BillOperateType.Add) && (EditStatus != Common.BillOperateType.Update);
            //btnOpen.Enabled = (EditStatus == Common.BillOperateType.None);
            btnSave.Enabled = bEdit;
            btnSelect.Enabled = bEdit;
            btnDelete.Enabled = bEdit;
            btnAddItem.Enabled = bEdit;
            btnDelItem.Enabled = bEdit;
            btnRapid.Enabled = bEdit;
            btnConfirm.Enabled = bConfirm;
            btnPrint.Enabled = bPrint;
        }

        private void RefreshStatus()
        {
            bEdit = (EditStatus == Common.BillOperateType.Add) || (EditStatus == Common.BillOperateType.Update);
            bConfirm = (tMain.Rows.Count > 0);
            if (bConfirm)
                bConfirm = (Common.StringToInt(tMain.Rows[0]["id"].ToString()) > 0);
            bPrint = (EditStatus != Common.BillOperateType.Add) && (EditStatus != Common.BillOperateType.Update);
        }


        private void SetControlsStatus(bool SetEnabledFlag)
        {
            //txtcode.ReadOnly = !SetEnabledFlag;
            dpReqDate.Enabled = SetEnabledFlag;
            //txtOperator.ReadOnly = !SetEnabledFlag;
            //txtAccessor.ReadOnly = !SetEnabledFlag;
            //txtStatus.ReadOnly = !SetEnabledFlag;
            deptSelector.Enabled = SetEnabledFlag;
            cbbbusinessMan.Enabled = SetEnabledFlag;
            //dpCreateTime.Enabled = SetEnabledFlag;
            //dpVerifyTime.Enabled = SetEnabledFlag;
            traderSelector.Enabled = SetEnabledFlag;
            //txtMender.ReadOnly = !SetEnabledFlag;
            //dpUpdateTime.Enabled = SetEnabledFlag;
            txtMemo.ReadOnly = !SetEnabledFlag;
            theView.ReadOnly = !SetEnabledFlag;

            //theView.SetAddStates(SetEnabledFlag);
            //指定表格中只读的字段，防止被用户错误修改
            if (SetEnabledFlag)
            {
                theView.Columns["MaterialName"].ReadOnly = SetEnabledFlag;
                theView.Columns["specs"].ReadOnly = SetEnabledFlag;
                theView.Columns["unit"].ReadOnly = SetEnabledFlag;
            }
        }

        private void OpenRequisition(int r)
        {
            XReqMain = BaseDataProxy.GetXRequisition(r);
            XDetials = BaseDataProxy.GetXReqDetails(r);
            //    //表头内容
            IEnumerable<XRequisition> ary = new List<XRequisition>() { XReqMain };
            tMain = ary.ConvertToDataTable<TB_Requisition>();
            //    //表体内容
            tDetail = XDetials.ConvertToDataTable<TB_ReqDetail>();

            if ((bool)tMain.Rows[0]["isVerify"] == true)
            {
                //审批后,不允许再修改
                EditStatus = Common.BillOperateType.View;
                SetControlsStatus(false);
            }
            else
            {
                //未审批,允许再修改
                tMain.Rows[0]["mender"] = SysLoginUserInfo.UserId;
                tMain.Rows[0]["MenderName"] = SysLoginUserInfo.UserName;
                tMain.Rows[0]["updateTime"] = DateTime.Today;
                EditStatus = Common.BillOperateType.Update;
                SetControlsStatus(true);
            }

            DataBindAll();
        }

        private void UpdateBizManList(int DepartmentId)
        {
            XUsers = BaseDataProxy.FindUsers(DepartmentId, "", "");

            //XUsers.copy order by user name
            //刷新部门对应业务员
            //IEnumerable<XUser> query = XUsers.OrderBy(XUser => XUser.User.name);

            cbbbusinessMan.Items.Clear();
            foreach (XUser dp in XUsers)
            {
                cbbbusinessMan.Items.Add(dp.User);
            }
            cbbbusinessMan.DisplayMember = "name";
            cbbbusinessMan.ValueMember = "id";
        }

        private void DataBindAll()
        {
            //数据绑定
            txtcode.Text = tMain.Rows[0]["code"].ToString();
            if (tMain.Rows[0]["reqDate"].ToString() != "")
                dpReqDate.Value = (DateTime)tMain.Rows[0]["reqDate"];
            txtOperator.Text = tMain.Rows[0]["OperatorName"].ToString();
            txtAccessor.Text = tMain.Rows[0]["AssessorName"].ToString();
            txtStatus.Text = tMain.Rows[0]["status"].ToString();
            if (tMain.Rows[0]["createtime"].ToString() != "")
                dpCreateTime.Value = (DateTime)tMain.Rows[0]["createtime"];

            if (tMain.Rows[0]["verifytime"].ToString() != "")
                dpVerifyTime.Value = (DateTime)tMain.Rows[0]["verifytime"];

            txtMender.Text = tMain.Rows[0]["MenderName"].ToString();

            if (tMain.Rows[0]["updatetime"].ToString() != "")
                dpUpdateTime.Value = (DateTime)tMain.Rows[0]["updatetime"];

            txtMemo.Text = tMain.Rows[0]["memo"].ToString();

            if (!string.IsNullOrEmpty(tMain.Rows[0]["trader"].ToString()))
            {
                SetTrader();
            }

            if (!string.IsNullOrEmpty(tMain.Rows[0]["department"].ToString()))
            {
                SetDepartment();
                UpdateBizManList((int)tMain.Rows[0]["department"]);
            }

            theView.DataSource = tDetail;
        }

        private void SetTrader()
        {
            SelectItem item = new SelectItem();
            Trader = BaseDataProxy.GetTrader((int)tMain.Rows[0]["trader"]);
            if (Trader != null)
            {
                item.Name = Trader.name;
                item.Key = Trader.code;
                item.Value = Trader.id.ToString();
                traderSelector.SetSelected(item);
            }
        }

        private void SetDepartment()
        {
            SelectItem item = new SelectItem();
            Department = BaseDataProxy.GetDepartment((int)tMain.Rows[0]["department"]);
            if (Department != null)
            {
                item.Name = Department.name;
                item.Key = Department.code;
                item.Value = Department.id.ToString();
                deptSelector.SetSelected(item);
            }
        }

        private void ClearCtrlData()
        {
            txtcode.Text = "";
            dpReqDate.Value = DateTime.Now;
            txtOperator.Text = "";
            txtAccessor.Text = "";
            txtStatus.Text = "";
            deptSelector.SetSelected(null);
            cbbbusinessMan.Text = "";
            dpCreateTime.Value = DateTime.Now;
            dpVerifyTime.Value = DateTime.Now;
            traderSelector.SetSelected(null);
            dpUpdateTime.Value = DateTime.Now;
            txtMemo.Text = "";
            theView.DataSource = null;

        }

        public RequisitionFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new BaseDataClient();
            this.BaseDataProxy.Open();

            this.codeRule = new CodeRuleFrm.CodeRule();
            EditStatus = Common.BillOperateType.None;

            BillPrefix = "QG";
        }

        private void RequisitionFrm_Load(object sender, EventArgs e)
        {
            theView = gvRequireDetail;

            SetControlsStatus(false);

            theView.Controls.Add(this._DTP);  //把时间控件加入DataGridView
            this._DTP.Visible = false;  //先不让它显示
            this._DTP.Format = DateTimePickerFormat.Custom;
            this._DTP.TextChanged += new EventHandler(dtp_TextChange); //为时间控件加入事件dtp_TextChange            

            theView.CellClick += new DataGridViewCellEventHandler(theView_CellClick);
            theView.CellDoubleClick += new DataGridViewCellEventHandler(theView_CellDoubleClick);
            theView.RowsAdded += new DataGridViewRowsAddedEventHandler(theView_RowsAdded);
            theView.RowsRemoved += new DataGridViewRowsRemovedEventHandler(theView_RowsRemoved);
            theView.CellFormatting += new DataGridViewCellFormattingEventHandler(theView_CellFormatting);
            theView.ColumnWidthChanged += new DataGridViewColumnEventHandler(theView_ColumnWidthChanged);
            theView.Scroll += new ScrollEventHandler(theView_Scroll);

            theView.AutoGenerateColumns = false;
        }

        private void btnAppend_Click(object sender, EventArgs e)
        {
            //if (EditStatus == Common.BillOperateType.Add)
            //{
            //    EditStatus = Common.BillOperateType.None;
            //    SetControlsStatus(false);
            //    theView.CancelEdit();
            //    return;
            //}
            EditStatus = Common.BillOperateType.Add;
            SetControlsStatus(true);

            tMain.Clear();
            DataRow NewMainRow = tMain.NewRow();
            // 开新单时，单号为空，保存时申请后显示
            NewMainRow["code"] = ""; //BaseDataProxy.GetSerialNo(1, BillPrefix, 4, true, true, true);
            NewMainRow["operator"] = SysLoginUserInfo.UserId;
            NewMainRow["OperatorName"] = SysLoginUserInfo.UserName;
            NewMainRow["reqDate"] = DateTime.Now;
            NewMainRow["createtime"] = DateTime.Now;
            NewMainRow["status"] = "未批";
            NewMainRow["isVerify"] = false;
            NewMainRow["isClosed"] = false;
            tMain.Rows.Add(NewMainRow);

            tDetail.Clear();
            DataRow NewDetailRow = tDetail.NewRow();
            NewDetailRow["rDate"] = DateTime.Now;
            NewDetailRow["pDate"] = DateTime.Now;
            NewDetailRow["reqnCode"] = tMain.Rows[0]["code"];
            tDetail.Rows.Add(NewDetailRow);

            DataBindAll();

            if (!string.IsNullOrEmpty(tMain.Rows[0]["trader"].ToString()))
            {
                SelectItem item = new SelectItem();
                Trader = BaseDataProxy.GetTrader((int)tMain.Rows[0]["trader"]);
                item.Name = Trader.name;
                item.Key = Trader.code;
                item.Value = Trader.id.ToString();
                traderSelector.SetSelected(item);
            }

            if (!string.IsNullOrEmpty(tMain.Rows[0]["department"].ToString()))
            {
                SelectItem item = new SelectItem();
                Department = BaseDataProxy.GetDepartment((int)tMain.Rows[0]["department"]);
                item.Name = Department.name;
                item.Key = Department.code;
                item.Value = Department.id.ToString();
                deptSelector.SetSelected(item);
            }
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            //加载打开单据
            FindBillFrm frm = new FindBillFrm();
            frm.AllowMulitSelect = false;
            frm.SelectBillType = GRWL.BZSC.Common.EnumCommon.BillType.QG;
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK)
            {
                if (frm.SelectedItems != null)
                {
                    QRequisition qre = OTConverter.ConvertToObject<QRequisition>(frm.SelectedItems);
                    //QQuotation qqu = OTConverter.ConvertToObject<QQuotation>(frm.SelectedItems);
                    OpenRequisition(qre.Id);
                }
            }

            //SelectBillFrm frm = new SelectBillFrm();
            //frm.SetCondition(GRWL.BZSC.Common.EnumCommon.BillType.QG, BillPrefix, false);
            //frm.ShowDialog();
            //if (frm.SelectBills.Count > 0)
            //{
            //    OpenRequisition(frm.SelectBills[0].BillId);
            //}

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if ((EditStatus != Common.BillOperateType.Add) & (EditStatus != Common.BillOperateType.Update) & (EditStatus == Common.BillOperateType.Delete))
                return;

            if (EditStatus == Common.BillOperateType.Add)
            {
                //tMain.Rows[0]["operator"] = SysLoginUserInfo.UserId;
                //tMain.Rows[0]["OperatorName"] = SysLoginUserInfo.UserName;
                //tMain.Rows[0]["createTime"] = DateTime.Now;
                if (string.IsNullOrEmpty(tMain.Rows[0]["code"].ToString()))
                {
                    tMain.Rows[0]["code"] = BaseDataProxy.GetSerialNo(1, BillPrefix, 4, true, true, true);
                    txtcode.Text = tMain.Rows[0]["code"].ToString();
                }
            }
            else
            {
                //tMain.Rows[0]["mender"] = SysLoginUserInfo.UserId;
                //tMain.Rows[0]["MenderName"] = SysLoginUserInfo.UserName;
                //tMain.Rows[0]["updateTime"] = DateTime.Now;
            }

            if (cbbbusinessMan.SelectedItem == null)
            {
                tMain.Rows[0]["businessman"] = DBNull.Value;
                tMain.Rows[0]["BusinessName"] = "";
            }
            else
            {
                tMain.Rows[0]["businessman"] = ((TB_User)cbbbusinessMan.SelectedItem).id;
                tMain.Rows[0]["BusinessName"] = ((TB_User)cbbbusinessMan.SelectedItem).name;
            }

            //tMain.Rows[0]["code"] = txtcode.Text.Trim();
            tMain.Rows[0]["reqDate"] = dpReqDate.Value;
            tMain.Rows[0]["memo"] = txtMemo.Text.Trim();
            tMain.AcceptChanges();

            if (theView.Rows.Count == 0)
            {
                MessageBox.Show("单据明细记录为空不可以保存。");
                return;
            }

            foreach (DataGridViewRow TheRow in theView.Rows)
            {
                if ((string.IsNullOrEmpty(TheRow.Cells["material"].Value.ToString())) || (string.IsNullOrEmpty(TheRow.Cells["materialCode"].Value.ToString())))
                {
                    theView.CurrentCell = TheRow.Cells["materialCode"];
                    MessageBox.Show("输入的物资不正确，请重新输入。");
                    return;
                }
            }

            foreach (DataRow dr in tDetail.Rows)
            {
                if (string.IsNullOrEmpty(dr["reqncode"].ToString()))
                    dr["reqncode"] = tMain.Rows[0]["code"];
            }
            tDetail.AcceptChanges();

            TB_Requisition Requistion = OTConverter.ConvertToObject<TB_Requisition>(tMain.Rows[0]);

            List<TB_ReqDetail> LReqDetails = OTConverter.ConvertTableToObject<TB_ReqDetail>(theView.DataSource as DataTable);
            TB_ReqDetail[] ReqDetails = LReqDetails.ToArray();
            if (EditStatus == Common.BillOperateType.Add)
            {
                tMain.AcceptChanges();
                Requistion.TB_ReqDetail = ReqDetails;
                int iAddID = BaseDataProxy.AddRequisition(Requistion);
                tMain.Rows[0]["id"] = iAddID;
                tMain.AcceptChanges();
                if (iAddID > 0)
                {
                    EditStatus = Common.BillOperateType.Update;
                    MessageBox.Show("已成功保存单据.");
                    OpenRequisition((int)tMain.Rows[0]["id"]);
                }
                else
                    MessageBox.Show("保存单据失败.");
            }
            else // (EditStatus == Common.BillOperateType.Update)
            {
                Requistion.TB_ReqDetail = ReqDetails;
                if (BaseDataProxy.UpdateRequisition(Requistion, true))
                {
                    MessageBox.Show("已成功修改单据.");
                    OpenRequisition((int)tMain.Rows[0]["id"]);
                }
                else
                    MessageBox.Show("保存单据失败.");
            }
        }

        private void ImportBill(List<SelectBillFrm.SelectBillsInfo> list)
        {

            XReqDetial[] xImportBill = BaseDataProxy.GetXReqDetails(2);
            DataTable tbImport = new DataTable();
            tbImport = OTConverter.GenericListToDataTable(xImportBill);
            if (tbImport == null || tbImport.Rows == null)
                return;

            theView.DataSource = tDetail;//选单时先绑定数据

            foreach (DataRow rSel in tbImport.Rows)
            {
                if (theView.DataSource == null)//当无数据时 首次添加时
                    theView.Rows.Add(1);
                else //有数据 进行修改
                    ((DataTable)theView.DataSource).Rows.Add("");
                theView.Rows[theView.Rows.Count - 1].Cells["material"].Value = rSel["material"].ToString(); //材料单号
                theView.Rows[theView.Rows.Count - 1].Cells["materialCode"].Value = rSel["materialCode"].ToString();
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            SelectBillFrm frm = new SelectBillFrm();
            frm.SetCondition(GRWL.BZSC.Common.EnumCommon.BillType.QG, BillPrefix, true);
            frm.ShowDialog();

            ImportBill(frm.SelectBills);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (theView.CurrentCell == null || theView.CurrentCell.RowIndex < 0) return;

            //if (!string.IsNullOrEmpty(tMain.Rows[0]["assessor"].ToString()))
            if ((bool)tMain.Rows[0]["isVerify"] == true)
            {
                MessageBox.Show("无法删除已审批单据.");
                return;
            }

            if ((EditStatus != Common.BillOperateType.Add) & (EditStatus != Common.BillOperateType.Update))
                return;

            //theView.CancelEdit();

            //if (EditStatus == Common.BillOperateType.Add)
            //{
            //    tMain.Rows.Clear();
            //    tDetail.Rows.Clear();
            //    return;
            //}

            //(EditStatus == Common.BillOperateType.Update)
            int BillId = Common.StringToInt(tMain.Rows[0]["id"].ToString());
            string message = "是否删除单据？";
            string caption = "确认";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result = MessageBox.Show(message, caption, buttons);
            if (result == DialogResult.Yes)
            {
                if (BaseDataProxy.DeleteRequisition(BillId))
                {
                    MessageBox.Show("已成功删除单据");
                    ClearCtrlData();//清除控件数据
                    EditStatus = Common.BillOperateType.None;
                }
                else
                    MessageBox.Show("无法删除单据.");
            }
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            //if (theView.DataSource == null)//当无数据时 首次添加时
            //    theView.Rows.Add(1);
            //else //有数据 进行修改
            //    ((DataTable)theView.DataSource).Rows.Add("");
            //新行默认值设定
            DataRow dr = tDetail.NewRow();
            dr["rDate"] = DateTime.Now;
            dr["pDate"] = DateTime.Now;
            dr["reqnCode"] = tMain.Rows[0]["code"];
            tDetail.Rows.Add(dr);
        }

        private void btnDelItem_Click(object sender, EventArgs e)
        {
            if (theView.RowCount > 0)
            {

                theView.Rows.RemoveAt(theView.CurrentRow.Index);
                tDetail.AcceptChanges();

                // 删除所有记录后，自动建立一个空白记录
                if (theView.RowCount == 0)
                {
                    btnAddItem_Click(null, null);
                    tDetail.AcceptChanges();
                }
            }
        }

        private void btnRapid_Click(object sender, EventArgs e)
        {
            GetMaterial();
        }

        private void GetMaterial()
        {
            FindMaterialFrm frm = new FindMaterialFrm();
            frm.Text = "选择物资";
            if (frm.ShowDialog() == DialogResult.OK)
            {
                DataGridViewRow dr = theView.CurrentRow;
                dr.Cells["material"].Value = frm.SelectorItem.Id;
                dr.Cells["materialCode"].Value = frm.SelectorItem.Code;
                dr.Cells["unit"].Value = frm.SelectorItem.Unit;
                //dr.Cells["brand"].Value = frm.SelectorItem.Brand;
                dr.Cells["specs"].Value = frm.SelectorItem.Specs;
                dr.Cells["MaterialName"].Value = frm.SelectorItem.CnName;
                return;
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            EditStatus = Common.BillOperateType.None;
            this.Close();
        }


        private void traderSelector_SelectorClick(object sender, EventArgs e)
        {
            TraderFrm frm = new TraderFrm();
            frm.Text = "选择供应商";
            frm.SearchSupplierMode(); // .SetTraderAsSupplier();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                traderSelector.SetSelected(frm.SelectorItem);
                tMain.Rows[0]["trader"] = Common.StringToInt(frm.SelectorItem.Value);
            }
        }

        private void deptSelector_SelectorClick(object sender, EventArgs e)
        {
            DeptFrm frm = new DeptFrm();
            frm.Text = "选择部门";
            if (frm.ShowDialog() == DialogResult.OK)
            {
                deptSelector.SetSelected(frm.SelectorItem);
                tMain.Rows[0]["department"] = Common.StringToInt(frm.SelectorItem.Value);
                UpdateBizManList(int.Parse(frm.SelectorItem.Key.ToString()));
            }

        }

        private void theView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex < 0)
                return;

            if ((EditStatus != Common.BillOperateType.Add) & (EditStatus != Common.BillOperateType.Update))
            {
                return;
            }

            UDataGridView TheView = (UDataGridView)sender;
            DataGridViewRow TheRow = TheView.CurrentRow; //.Rows[e.RowIndex];
            DataGridViewColumn TheCol = TheView.Columns[e.ColumnIndex];

            //物资编号 
            if ((TheCol.Name == "materialCode") || (TheCol.Name == "MaterialName"))
            {
                FindMaterialFrm frm = new FindMaterialFrm();
                frm.Text = "选择物资";
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    //theView.CurrentRow.
                    TheRow.Cells["material"].Value = frm.SelectorItem.Id;
                    TheRow.Cells["materialCode"].Value = frm.SelectorItem.Code;
                    TheRow.Cells["MaterialName"].Value = frm.SelectorItem.CnName;
                    TheRow.Cells["specs"].Value = frm.SelectorItem.Specs;
                    TheRow.Cells["unit"].Value = frm.SelectorItem.Unit;
                    TheRow.Cells["Expire"].Value = frm.SelectorItem.Expire;

                }
            }
        }

        void theView_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            Common.ShowDataGridViewRowNo((UDataGridView)sender);
        }

        void theView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            Common.ShowDataGridViewRowNo((UDataGridView)sender);
        }

        void theView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            Common.CellFormatColor(sender, e, EditStatus);
        }

        private void theView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            UDataGridView theView = (UDataGridView)sender;
            // 判断所在是否日期选择的列
            if (theView.Columns[e.ColumnIndex].ValueType == typeof(DateTime))
            {
                Rectangle _Rectangle = theView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); //得到所在单元格位置和大小
                this._DTP.Size = new Size(_Rectangle.Width, _Rectangle.Height); //把单元格大小赋给时间控件
                this._DTP.Location = new Point(_Rectangle.X, _Rectangle.Y); //把单元格位置赋给时间控件
                if ((!theView.CurrentCell.ReadOnly) && (!string.IsNullOrEmpty(theView.CurrentCell.Value.ToString())))
                    this._DTP.Value = (DateTime)theView.CurrentCell.Value;
                this._DTP.Visible = true;  //可以显示控件了
            }
            else
            {
                this._DTP.Visible = false;
            }
        }

        private void theView_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
        {
            this._DTP.Visible = false;
        }

        private void theView_Scroll(object sender, ScrollEventArgs e)
        {
            this._DTP.Visible = false;
        }

        private void dtp_TextChange(object sender, EventArgs e)
        {
            theView.CurrentCell.Value = this._DTP.Text.ToString();  //时间控件选择时间时，就把时间赋给所在的单元格
        }

        private void cbbbusinessMan_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sBizManName = cbbbusinessMan.Text;
            IEnumerable<XUser> rDp = from dp in XUsers
                                     where dp.User.name == sBizManName
                                     select dp;

            tMain.Rows[0]["businessman"] = rDp.First().User.id;
        }

        private void btnConfirm_ButtonClick(object sender, EventArgs e)
        {
            btnConfirm.ShowDropDown();

        }

        private void UpdateStatus(int BillId)
        {
            XRequisition xMain = BaseDataProxy.GetXRequisition(BillId);
            if (xMain.Requisition.assessor == null)
                tMain.Rows[0]["assessor"] = DBNull.Value;
            else
                tMain.Rows[0]["assessor"] = xMain.Requisition.assessor;
            if (xMain.Requisition.verifyTime == null)
                tMain.Rows[0]["verifyTime"] = DBNull.Value;
            else
                tMain.Rows[0]["verifyTime"] = xMain.Requisition.verifyTime;
            tMain.Rows[0]["status"] = xMain.Requisition.status;
            tMain.Rows[0]["AssessorName"] = xMain.AssessorName;
            tMain.AcceptChanges();

            txtAccessor.Text = xMain.AssessorName;
            dpVerifyTime.Text = xMain.Requisition.verifyTime.ToString();
            txtStatus.Text = xMain.Requisition.status;

        }

        private void btnConfirmOk_Click(object sender, EventArgs e)
        {
            try
            {
                int BillId = Common.StringToInt(tMain.Rows[0]["ID"].ToString());
                VerifyBillResult vbResult = BaseDataProxy.VerifyBill(BillPrefix, BillId, SysLoginUserInfo.UserId, true);
                MessageBox.Show(vbResult.Message);
                if (vbResult.Pass)
                {
                    //UpdateStatus(BillId);
                    //EditStatus = Common.BillOperateType.View;
                    //SetControlsStatus(false);
                    //theView.Refresh();
                    OpenRequisition((int)tMain.Rows[0]["id"]);
                }
            }
            catch
            {
            }

        }

        private void btnConfirmCancel_Click(object sender, EventArgs e)
        {
            try
            {
                int BillId = Common.StringToInt(tMain.Rows[0]["ID"].ToString());
                VerifyBillResult vbResult = BaseDataProxy.VerifyBill(BillPrefix, BillId, SysLoginUserInfo.UserId, false);
                MessageBox.Show(vbResult.Message);
                if (vbResult.Pass)
                {
                    //UpdateStatus(BillId);
                    //EditStatus = Common.BillOperateType.Update;
                    //SetControlsStatus(true);
                    //theView.Refresh();
                    OpenRequisition((int)tMain.Rows[0]["id"]);
                }
            }
            catch
            {
            }
        }

        private void deptSelector_SelectorTextUpdate(object sender, EventArgs e)
        {

        }

        private void traderSelector_SelectorTextUpdate(object sender, EventArgs e)
        {
            //TB_Trader[] datas = this.BaseDataProxy.FindTraders(null, true, null, null, this.traderSelector.GetComboBoxText(), null);
            TB_Trader[] datas = BaseDataProxy.FindTraders(null, true, null, null, null, (sender as Selector).GetComboBoxText());
            (sender as Selector).ReBindComboBox<TB_Trader>(datas, "id", "name", "code");
        }

    }
}
